using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace FluentTags
{

    public class TRTag : Tag<TRTag>
    {
        public TRTag() : base("tr"){}
        
        public TRTag(string text) : this()
        {
            Controls.Add(new LiteralControl(text));
        }

        public TRTag(object value) : this()
        {
            Controls.Add(new LiteralControl(SafeString(value)));
        }
        
        
        public TRTag(Control control) : this()
        {
            Controls.Add(control);
        }
        
        
        
        
        public TDTag<TRTag> TD()
        {
            var ctrl = new TDTag<TRTag>(this);
            Controls.Add(ctrl);
            return ctrl;
        }

        public TRTag TD(TDTag child)
        {
            Controls.Add(child);
            return this;
        }
        
        public TDTag<TRTag> TD(string text)
        {
            var ctrl = new TDTag<TRTag>(this);
            Controls.Add(ctrl);
            LiteralControl lc = new LiteralControl(text);
            ctrl.Controls.Add(lc);
            
            return ctrl;
        }

        public TDTag<TRTag> TD(Control control)
        {
            var ctrl = new TDTag<TRTag>(this);
            Controls.Add(ctrl);
            ctrl.Controls.Add(control);
            
            return ctrl;
        }

        public TDTag<TRTag> TD(object value)
        {
            return TD(SafeString(value));
        }
        
        public THTag<TRTag> TH()
        {
            var ctrl = new THTag<TRTag>(this);
            Controls.Add(ctrl);
            return ctrl;
        }

        public TRTag TH(THTag child)
        {
            Controls.Add(child);
            return this;
        }
        
        public THTag<TRTag> TH(string text)
        {
            var ctrl = new THTag<TRTag>(this);
            Controls.Add(ctrl);
            LiteralControl lc = new LiteralControl(text);
            ctrl.Controls.Add(lc);
            
            return ctrl;
        }

        public THTag<TRTag> TH(Control control)
        {
            var ctrl = new THTag<TRTag>(this);
            Controls.Add(ctrl);
            ctrl.Controls.Add(control);
            
            return ctrl;
        }

        public THTag<TRTag> TH(object value)
        {
            return TH(SafeString(value));
        }
        
        
        public TRTag Text(string text)
        {
            Controls.Add(new LiteralControl(text));
            return this;
        }

        public TRTag Text(object value)
        {
            Controls.Add(new LiteralControl(SafeString(value)));
            return this;
        }
        
    }

    public class TRTag<ParentType> : ChildTag<TRTag<ParentType>, ParentType>
        where ParentType : HtmlGenericControl
    {
        public TRTag(ParentType parent) : base("tr", parent) { }

        
        
        
        public TDTag<TRTag<ParentType>> TD()
        {
            var ctrl = new TDTag<TRTag<ParentType>>(this);
            Controls.Add(ctrl);
            return ctrl;
        }

        public TRTag<ParentType> TD(TDTag child)
        {
            Controls.Add(child);
            return this;
        }
        
        public TDTag<TRTag<ParentType>> TD(string text)
        {
            var ctrl = new TDTag<TRTag<ParentType>>(this);
            LiteralControl lc = new LiteralControl(text);
            Controls.Add(ctrl);
            ctrl.Controls.Add(lc);
            
            return ctrl;
        }

        public TDTag<TRTag<ParentType>> TD(Control control)
        {
            var ctrl = new TDTag<TRTag<ParentType>>(this);
            Controls.Add(ctrl);
            ctrl.Controls.Add(control);
            
            return ctrl;
        }

        public TDTag<TRTag<ParentType>> TD(object value)
        {
            var ctrl = new TDTag<TRTag<ParentType>>(this);
            LiteralControl lc = new LiteralControl(SafeString(value));
            Controls.Add(ctrl);
            ctrl.Controls.Add(lc);
            return ctrl;
        }
        
        public THTag<TRTag<ParentType>> TH()
        {
            var ctrl = new THTag<TRTag<ParentType>>(this);
            Controls.Add(ctrl);
            return ctrl;
        }

        public TRTag<ParentType> TH(THTag child)
        {
            Controls.Add(child);
            return this;
        }
        
        public THTag<TRTag<ParentType>> TH(string text)
        {
            var ctrl = new THTag<TRTag<ParentType>>(this);
            LiteralControl lc = new LiteralControl(text);
            Controls.Add(ctrl);
            ctrl.Controls.Add(lc);
            
            return ctrl;
        }

        public THTag<TRTag<ParentType>> TH(Control control)
        {
            var ctrl = new THTag<TRTag<ParentType>>(this);
            Controls.Add(ctrl);
            ctrl.Controls.Add(control);
            
            return ctrl;
        }

        public THTag<TRTag<ParentType>> TH(object value)
        {
            var ctrl = new THTag<TRTag<ParentType>>(this);
            LiteralControl lc = new LiteralControl(SafeString(value));
            Controls.Add(ctrl);
            ctrl.Controls.Add(lc);
            return ctrl;
        }
        
        
        public TRTag<ParentType> Text(string text)
        {
            Controls.Add(new LiteralControl(text));
            return this;
        }
        
        public TRTag<ParentType> Text(object value)
        {
            Controls.Add(new LiteralControl(SafeString(value)));
            return this;
        }
        
        
    }
}